<?php

class Core_Backend_Component_Subject_View_Content_Index extends Core_Backend_Share_View_Grid {

    protected function buildToolbar($toolbar) {
        parent::buildToolbar($toolbar);
        if ($this->getIdentity()->isAllowAction('subject_reviewer')) {
            $group = $toolbar->createGroup('status');
            $group->addButton($this->__('share.button.publish'))
                    ->bindEvent('click', "submitGridChecked('" . $this->getGrid()->getId() . "', '" . $this->getUrl(array('status'=>'published'), 'review') . "')");
            $group->addButton($this->__('share.button.forbid'))
                    ->bindEvent('click', "forbidGridChecked('" . $this->getGrid()->getId() . "', '" . $this->getUrl(array('status'=>'forbidden'), 'review') . "')");
        }
        if ($this->getIdentity()->isAllowAction('subject_delete')) {
            $group = $toolbar->createGroup('delete');
            $group->addButton($this->__('share.button.delete'))
                    ->bindEvent('click', "submitGridChecked('" . $this->getGrid()->getId() . "', '" . $this->getUrl(NULL, 'delete') . "')");
        }
        $group = $toolbar->createGroup();
        $group->addLink($this->getUrl(NULL, 'create'), $this->__('share.button.create'));
//        if ($this->getIdentity()->isInRole('subject_manager')) {
//            $group->addLink($this->getUrl(NULL, 'generate-tag'), $this->__('share.button.generate_tag'));
//        }
    }

    protected function buildGrid($grid) {
        $filter = new Kd_Object($this->getRequest()->getParameter('filter'));
        $grid->setId('content_grid')
                ->setPrimaryKey('id')
                ->setAllowFilter(TRUE)
                ->setSortField($this->getRequest()->getParameter('sort', 'id'))
                ->setSortDirection($this->getRequest()->getParameter('dir', 'asc'))
                ->setSortUrlFormat($this->getSortUrl());
        $grid->addLabelColumn($this->__('share.label.name'), 'name', '{name}')
                ->setTextboxFilter(NULL, NULL, $filter->getData('title'))
                ->setSortField('title')
                ->setWidth(200);
        $grid->addLabelColumn($this->__('subject.label.click_count'), 'clickcount', '{click_count}')
                ->setSortField('clickcount')
                ->setWidth(60);
        $grid->addLabelColumn($this->__('subject.label.hot'), 'hot', '{is_hot}')
                ->setSelectFilter(NULL, NULL, Kd_Localize_Helper::getLanguageItems('status.boolean'), NULL, NULL, $filter->getData('hot'))
                ->setSortField('hot')
                ->setWidth(60);
        $grid->addLabelColumn($this->__('subject.label.new'), 'new', '{is_new}')
                ->setSelectFilter(NULL, NULL, Kd_Localize_Helper::getLanguageItems('status.boolean'), NULL, NULL, $filter->getData('new'))
                ->setSortField('new')
                ->setWidth(60);
        $grid->addLabelColumn($this->__('subject.label.top'), 'top', '{is_top}')
                ->setSelectFilter(NULL, NULL, Kd_Localize_Helper::getLanguageItems('status.boolean'), NULL, NULL, $filter->getData('top'))
                ->setSortField('top')
                ->setWidth(60);
        $grid->addLabelColumn($this->__('share.label.tag'), 'tag', '{tag}')
                ->setTextboxFilter(NULL, NULL, $filter->getData('tag'));
        $grid->addLabelColumn($this->__('share.label.status'), 'status', '{status}')
                ->setSelectFilter(NULL, NULL, Kd_Localize_Helper::getLanguageItems('status.publication'), NULL, NULL, $filter->getData('status'))
                ->setSortField('status')
                ->setWidth(80);
        $grid->addLabelColumn($this->__('share.label.mender'), 'mender', '{mender_name}')
                ->setSortField('mender')
                ->setWidth(100);
        if ($this->getIdentity()->isInRole('subject_manager')) {
            $grid->addLinkColumn('#', 'edit', $this->getUrl(array('id' => '{id}'), 'edit'), $this->__('share.button.edit'))
                    ->setWidth(40)
                    ->setAlign('center');
        }
        $grid->addLinkColumn('#', 'article', $this->getUrl(array('id' => '{id}'), 'index','article','subject'), $this->__('subject.label.article'))
                ->setWidth(40)
                ->setAlign('center');
        $grid->addLinkColumn('#', 'concessions', $this->getUrl(array('id' => '{id}'), 'index','concessions','subject'), $this->__('subject.label.concessions'))
                ->setWidth(40)
                ->setAlign('center');
        $grid->addLinkColumn('#', 'delete', $this->getUrl(array('ids[]' => '{id}'), 'delete'), $this->__('share.button.delete'))
                ->setWidth(40)
                ->setAlign('center');
        $grid->setDataSource($this->getList()->toArray());
        parent::buildGrid($grid);
    }

    protected function buildPagination($pagination) {
        $url = $this->getPaginationUrl();
        $pagination->setUrlFormat($url)->setRecordCount($this->getRecords());
        parent::buildPagination($pagination);
    }

}